package com.oscar.ehome.view.messageview;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.oscar.ehome.R;
import com.oscar.ehome.base.activity.BasesActivity;
import com.oscar.ehome.bean.messageBean.MessageLeveBean;
import com.oscar.ehome.presenter.MessageListPreaenter;
import com.oscar.ehome.utils.basethem.TintManager;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.util.CommonUtils;
import com.oscar.ehome.utils.util.ToastUtil;
import com.oscar.ehome.utils.view.MyGridView;
import com.oscar.ehome.utils.view.ScrollEditText;
import com.oscar.ehome.view.loginview.LoginActivity;
import com.oscar.ehome.view.messageview.adapter.IssuemessageAdapter;
import com.oscar.ehome.view.messageview.view.IssueMessageView;
import com.oscar.ehome.view.setview.SettingActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by oscar
 * author: MengWanhe
 * date: On 2017/7/6
 * 发布消息页面
 */

public class IssueMessageActivity extends BasesActivity<IssueMessageView, MessageListPreaenter> implements IssueMessageView, IssuemessageAdapter.OnClickedlister {

    /**
     * 返回
     */
    @BindView(R.id.title_img_back)
    ImageView titleImgBack;
    /**
     * 返回
     */
    @BindView(R.id.title_layout_left)
    RelativeLayout titleLayoutLeft;
    /**
     * 标题
     */
    @BindView(R.id.title_topbar)
    TextView titleTopbar;
    /**
     * 输入的标题
     */
    @BindView(R.id.message_inform_titles)
    EditText messageInformTitles;
    /**
     * 通知内容
     */
    @BindView(R.id.message_inform_contexts)
    ScrollEditText messageInformContexts;
    /**
     * 发布消息
     */
    @BindView(R.id.inform_buton)
    Button informButon;
    /**
     * 职级
     */
    @BindView(R.id.leve_gridview)
    MyGridView leveGridview;
    /**
     * 通知范围
     */
    @BindView(R.id.tongzhi_fanwei_text)
    TextView tongzhiFanweiText;
    /**
     * title布局
     */
    @BindView(R.id.basetitle_bar)
    RelativeLayout basetitleBar;

    //网络请求
    MessageListPreaenter messageListPreaenter;
    String token = "";

    //数据源
    List<MessageLeveBean.ListBean> list = new ArrayList<>();//职级信息
    List<String> listdata = new ArrayList<>();//职级ID

    String foreignIds = "";//职级id
    //adapter
    IssuemessageAdapter adapter;

    int allCheck = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_issuemessage);
        ButterKnife.bind(this);
        setColor();
        initView();
        initData();
    }

    @Override
    protected MessageListPreaenter createPresenter() {
        messageListPreaenter = new MessageListPreaenter(this);
        return messageListPreaenter;
    }

    /**
     * 设置颜色
     */
    public void setColor() {
        basetitleBar.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(IssueMessageActivity.this).getSystemConfig().getColor()));
        TintManager.setTint(informButon, Color.parseColor(SystemConfigFactory.getInstance(this).getSystemConfig().getColor()));
    }

    /**
     * 初始化
     */
    public void initView() {

        titleImgBack.setVisibility(View.VISIBLE);
        titleImgBack.setImageResource(R.mipmap.fanhui);
        titleTopbar.setText(getString(R.string.inform_notices));
        token = SystemConfigFactory.getInstance(IssueMessageActivity.this).getSystemConfig().getToken();
        adapter = new IssuemessageAdapter(IssueMessageActivity.this, list);
        leveGridview.setAdapter(adapter);
        adapter.setOnClickedListener(this);
    }

    /**
     * 加载职级
     */
    public void initData() {
        showLoading();
        messageListPreaenter.getMessageLeve(token);
    }


    @OnClick({R.id.inform_buton, R.id.title_layout_left})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.inform_buton://发布
                fabuData();
                break;
            case R.id.title_layout_left:
                finish();
                break;
        }
    }

    /**
     * 发布通知
     */
    public void fabuData() {
        if (messageInformTitles.getText().toString().equals("") || messageInformTitles.getText().toString() == null) {
            ToastUtil.show(IssueMessageActivity.this, "请输入标题！");
            return;
        }
        if (messageInformContexts.getText().toString().equals("") || messageInformContexts.getText().toString() == null) {
            ToastUtil.show(IssueMessageActivity.this, "请输入内容！");
            return;
        }
        if (listdata.size() == 0) {
            ToastUtil.show(IssueMessageActivity.this, "请选择通知范围！");
            return;
        }
        foreignIds = CommonUtils.ListToString(listdata, 1);


        showLoading();
        messageListPreaenter.setMessage(token, messageInformTitles.getText().toString(), messageInformContexts.getText().toString(), foreignIds);
    }

    @Override
    public void setMessage(String result) {
        dismissLoading();
        ToastUtil.show(IssueMessageActivity.this, result);
        if (result.contains("成功")) {
            CommonUtils.pop = 2;
            finish();
        }
    }

    @Override
    public void getMessageLeve(MessageLeveBean result) {

        dismissLoading();
        list.clear();
        list = result.getList();
        if (result != null) {
            adapter.setList(list, 2);
        }
        if (list.size() == 0) {
            leveGridview.setVisibility(View.GONE);
            tongzhiFanweiText.setVisibility(View.GONE);
        } else {
            tongzhiFanweiText.setVisibility(View.VISIBLE);
            leveGridview.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void erroy() {
        dismissLoading();
        ToastUtil.show(IssueMessageActivity.this, getString(R.string.erry));
        finish();
    }

    @Override
    public void erry(String result) {
        dismissLoading();
        ToastUtil.show(IssueMessageActivity.this, result);
        if (result.contains("过期")) {
            getSharedPreferences("appbase", Context.MODE_PRIVATE).edit().clear().commit();
            SystemConfigFactory.getInstance(this).getSystemConfig().SetLogin(false);
            SystemConfigFactory.getInstance(this).getSystemConfig().setIsFirst(true);
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
        }
        finish();
    }

    @Override
    public void OnClick(int postion) {
        if (postion == list.size()) {//是全部的时候全部添加
            if (listdata.size() == list.size()) {//已经全选，全取消奧
                listdata.clear();//先清空
                adapter.setList(list, 0);
                allCheck = 0;//取消全选
            } else {//全选
                if (listdata.size() > 0) {
                    listdata.clear();//先清空
                }
                for (int i = 0; i < list.size(); i++) {
                    listdata.add(list.get(i).getId());
                }
                adapter.setList(list, 1);
                allCheck = 1;//全选
            }
        } else {//是去判断是第一次添加还是去除勾选
            String id = list.get(postion).getId();
            if (list.size() == listdata.size() || allCheck == 1) {
                for (int i = 0; i < listdata.size(); i++) {
                    if (id.equals(listdata.get(i))) {
                        listdata.remove(i);
                        break;
                    }
                }
            } else {
                if (listdata.size() > 0 && allCheck == 0) {
                    for (int i = 0; i < listdata.size(); i++) {
                        if (id.equals(listdata.get(i))) {
                            listdata.remove(i);
                            break;
                        } else {
                            listdata.add(id);
                            break;
                        }
                    }
                } else {
                    listdata.add(id);
                }
            }
        }
    }
}
